package com.linktel.productionLine.service;

import com.linktel.productionLine.common.ErrorCode;
import com.linktel.productionLine.dao.BasePedigreeDao;
import com.linktel.productionLine.dao.WorksheetPreserveDao;
import com.linktel.productionLine.dao.WorksheetYieldInfoDao;
import com.linktel.productionLine.entity.BasePedigree;
import com.linktel.productionLine.entity.dto.workSheetPreserve.WorkSheetPreserveDTO;
import com.linktel.productionLine.entity.query.workSheetPreserve.AddWorkSheetPreserveQuery;
import com.linktel.productionLine.entity.query.workSheetPreserve.EditWorkSheetPreserveQuery;
import com.linktel.productionLine.exception.BusinessException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@AllArgsConstructor
public class WorkSheetPreserveService {
    private WorksheetPreserveDao worksheetPreserveDao;

    private WorksheetYieldInfoDao worksheetYieldInfoDao;

    private BasePedigreeDao basePedigreeDao;


    public List<WorkSheetPreserveDTO> listWorkSheetPreserve() {
        return worksheetPreserveDao.listWorkSheetPreserve();
    }

    /**
     * 新增工单维护信息
     *
     * @param addWorkSheetPreserveQuery 新增的工单维护信息
     * @return 是否新增成功
     */
    public Boolean addWorkSheetPreserve(AddWorkSheetPreserveQuery addWorkSheetPreserveQuery) {
        String productCode = addWorkSheetPreserveQuery.getProductCode();
        String wsSerialNumber = addWorkSheetPreserveQuery.getWsSerialNumber();
        Integer hidden = addWorkSheetPreserveQuery.getHidden();
        Long currentUserId = addWorkSheetPreserveQuery.getCurrentUserId();
        if (currentUserId == null || currentUserId <= 0 || hidden == null || productCode == null || "".equals(productCode)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数不完整");
        }
        List<Long> workSheetIdList = worksheetPreserveDao.getByWsSerialNumberAndProductCode(wsSerialNumber, productCode);
        if (!CollectionUtils.isEmpty(workSheetIdList)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "相同的工单号和产品代码只能有一条数据");
        }
        BasePedigree basePedigree = basePedigreeDao.getByCode(productCode);
        String basePedigreeName = basePedigree.getName();
        Long basePedigreeId = basePedigree.getId();
        Long workSheetId = null;
        if (wsSerialNumber != null && !"".equals(wsSerialNumber)) {
            workSheetId = worksheetYieldInfoDao.getWorkSheetIdByNumber(wsSerialNumber);
        }
        return worksheetPreserveDao.addWorkSheetPreserve(workSheetId, wsSerialNumber, basePedigreeId, productCode, basePedigreeName, hidden, currentUserId);

    }

    /**
     * 删除工单维护信息
     *
     * @param currentUserId 当前登录用户id
     * @param id            工单维护信息id
     * @return 是否删除成功
     */
    public Boolean deleteWorkSheetPreserve(Long currentUserId, Long id) {
        if (id == null || id <= 0 || currentUserId == null || currentUserId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不能为空");
        }
        return worksheetPreserveDao.deleteWorkSheetPreserve(currentUserId, id);

    }

    @Transactional
    public Boolean editWorkSheetPreserve(EditWorkSheetPreserveQuery editWorkSheetPreserveQuery) {
        Long id = editWorkSheetPreserveQuery.getId();
        String productCode = editWorkSheetPreserveQuery.getProductCode();
        String wsSerialNumber = editWorkSheetPreserveQuery.getWsSerialNumber();
        Integer hidden = editWorkSheetPreserveQuery.getHidden();
        Long currentUserId = editWorkSheetPreserveQuery.getCurrentUserId();

        if (id == null || id <= 0 || currentUserId == null || currentUserId <= 0 || productCode == null || "".equals(productCode)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不能为空");
        }
        BasePedigree basePedigree = basePedigreeDao.getByCode(productCode);
        String basePedigreeName = basePedigree.getName();
        Long basePedigreeId = basePedigree.getId();
        Long workSheetId = null;
        if (wsSerialNumber != null && !"".equals(wsSerialNumber)) {
            workSheetId = worksheetYieldInfoDao.getWorkSheetIdByNumber(wsSerialNumber);
        }
        worksheetPreserveDao.editWorkSheetPreserve(id, workSheetId, wsSerialNumber, basePedigreeId, productCode, basePedigreeName, hidden, currentUserId);
        List<Long> workSheetIdList = worksheetPreserveDao.getByWsSerialNumberAndProductCode(wsSerialNumber, productCode);
        if (workSheetIdList.size() >= 2) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "一个工单号只能有一条数据");
        }
        return true;
    }
}
